//
//  HLMessageViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/20.
//

#import "HLMessageViewController.h"
#import <JXCategoryView/JXCategoryView.h>
#import "HLMessageListViewController.h"
#import "HLMessageSearchUserViewController.h"
#import "HLMessageRecordListViewController.h"
#import "HLIntimacyListViewController.h"
#import "HLDiscountRequest.h"
#import "HLHomeDiscountView.h"

@interface HLMessageViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray<NSString *>*labels;

@property (nonatomic, strong) QMUIButton *searchButton;
@property (nonatomic, strong) QMUIButton *loveBellButton;

@property (nonatomic, strong) HLHomeDiscountView *discountView;

@end

@implementation HLMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.categoryView.contentScrollView.scrollEnabled = NO;
    [self setupDiscountView];
}


- (void)setupDiscountView
{
    if ([HLRunTime shareInstance].isFood) {
        return;
    }
    
    HLDiscountRequest *homeListRequest = [[HLDiscountRequest alloc] init];
    [homeListRequest startWithCompletionBlockWithSuccess:^(__kindof HLDiscountRequest * _Nonnull request) {
        if (request.isSuccess){
            self.discountView = [[HLHomeDiscountView alloc] init];
            [self.view addSubview:self.discountView];
            [self.discountView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(140, 68));
                make.bottom.mas_equalTo(-(SafeAreaInsetsConstantForDeviceWithNotch.bottom + 68));
            }];
            self.discountView.cycleDatas = [request discountList];
        }
    } failure:nil];
}

- (void)initSubviews
{
    [super initSubviews];
    
    self.labels = [HLRunTime shareInstance].isFood ? @[@"消息"] :@[@"消息", @"视频通话", @"亲密度"];
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectZero];
    self.categoryView.delegate = self;
    self.categoryView.titleColor = HLTextColor2;
    self.categoryView.titleSelectedColor = HLTextColor1;
    self.categoryView.titleFont = UIFontMake(16);
    self.categoryView.titleSelectedFont = UIFontMediumMake(20);
    self.categoryView.averageCellSpacingEnabled = NO;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.titleLabelStrokeWidthEnabled = YES;
    self.categoryView.cellSpacing = 25;
    self.categoryView.contentEdgeInsetLeft = 18;
    self.categoryView.titles = self.labels;
    self.categoryView.backgroundColor = UIColorClear;
    [self.view addSubview:self.categoryView];
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-48);
        make.height.mas_equalTo(38);
        make.top.mas_equalTo(StatusBarHeightConstant + 4);
    }];
    
    self.searchButton = [[QMUIButton alloc] init];
    [_searchButton setImage:UIImageMake(@"message_search_icon") forState:UIControlStateNormal];
    [_searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_searchButton];
    [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(_categoryView);
    }];
    
    self.loveBellButton = [[QMUIButton alloc] init];
    [_loveBellButton setImage:UIImageMake(@"message_love_bell_close") forState:UIControlStateNormal];
    [_loveBellButton setImage:UIImageMake(@"message_love_bell_open") forState:UIControlStateSelected];
    [_loveBellButton addTarget:self action:@selector(loveBellAction:) forControlEvents:UIControlEventTouchUpInside];
    _loveBellButton.selected = [HLHelper loveBell];
    [self.view addSubview:_loveBellButton];
    [_loveBellButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_searchButton);
        make.right.equalTo(_searchButton.mas_left).offset(-12);
    }];
    
    
    self.categoryView.titleColorGradientEnabled = YES;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorClear;
    lineView.indicatorHeight = 5.f;
    lineView.indicatorWidth = 12.f;
    lineView.indicatorCornerRadius = 1.5f;
    lineView.verticalMargin = 2;
    lineView.lineStyle = JXCategoryIndicatorLineStyle_Lengthen;
    UIImageView *bottom = [[UIImageView alloc] initWithImage:UIImageMake(@"tag_bottom")];
    [lineView addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    self.categoryView.indicators = @[lineView];

    
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_categoryView.mas_bottom).offset(2);
        make.bottom.mas_equalTo(-TabBarHeight);
    }];
    
    self.categoryView.listContainer = self.listContainerView;
}

- (void)searchAction:(QMUIButton *)button
{
    HLMessageSearchUserViewController *searchUserVC = [[HLMessageSearchUserViewController alloc] init];
    [self.navigationController pushViewController:searchUserVC animated:YES];
}


- (void)loveBellAction:(QMUIButton *)button
{
    button.selected = !button.selected;
    [HLHelper toggleLoveBell];
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}

#pragma mark - JXCategoryViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.labels.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if (index ==  0){
        HLMessageListViewController *messageListVC = [[HLMessageListViewController alloc] init];
        return messageListVC;
    } else if (index == 1) {
        HLMessageRecordListViewController *recordListVC = [[HLMessageRecordListViewController alloc] init];
        return recordListVC;
    } else {
        HLIntimacyListViewController *intimacyListVC = [[HLIntimacyListViewController alloc] init];
        return intimacyListVC;
    }

}


@end
